package com.sevenGroup.pawnshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.Utils.GetObjectByJson;
import com.sevenGroup.pawnshop.controller.form.SelectWarehousesForm;
import com.sevenGroup.pawnshop.controller.form.pageForm;
import com.sevenGroup.pawnshop.controller.form.WarehousesForm;
import com.sevenGroup.pawnshop.controller.info.WarehousesInfo;
import com.sevenGroup.pawnshop.dao.WarehouseStoreDao;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.WarehouseStore;
import com.sevenGroup.pawnshop.model.Warehouses;
import com.sevenGroup.pawnshop.dao.WarehousesDao;
import com.sevenGroup.pawnshop.service.WarehousesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@Service
public class WarehousesServiceImpl extends ServiceImpl<WarehousesDao, Warehouses> implements WarehousesService {

    @Resource
    private WarehousesDao warehousesDao;

    @Resource
    private WarehouseStoreDao warehouseStoreDao;

    @Override
    public void saveWarehouse(WarehousesForm warehousesForm) throws sevenGroupException {

        if (warehousesForm == null) {

            throw new sevenGroupException(40011, "参数为空");
        }
        //新增前根据仓库地址判断仓库是否存在
        Warehouses warehouse_address = warehousesDao.selectOne(new QueryWrapper<Warehouses>().eq("warehouse_address", warehousesForm.getWarehouseAddress()));
        if (warehouse_address != null && warehouse_address.getWarehouseId() != warehousesForm.getWarehouseId()) {
            throw new sevenGroupException(40020, "该仓库已存在");
        }
        //新增前根据仓库名称判断仓库是否存在
        Warehouses warehouse_name = warehousesDao.selectOne(new QueryWrapper<Warehouses>().eq("warehouse_name", warehousesForm.getWarehouseName()));
        if (warehouse_name != null && warehouse_name.getWarehouseId() != warehousesForm.getWarehouseId()) {
            throw new sevenGroupException(40020, "该仓库已存在");
        }

        //到这就直接新增即可
        int insert = warehousesDao.insert(CopyProperties.copyOne(warehousesForm, Warehouses.class));
        if (insert <= 0) {

            throw new sevenGroupException(40012, "新增失败");

        }
    }

    @Override
    public IPage selectAllWarehouses(pageForm pageForm) throws sevenGroupException {
        if (pageForm == null) {
            throw new sevenGroupException(40001, "参数为空");
        }
        pageForm.setPageNum(pageForm.getPageNum() == null ? 1 : pageForm.getPageNum());
        pageForm.setPageSize(pageForm.getPageSize() == null ? 5 : pageForm.getPageSize());
        //将查询出的集合转为需要的集合后设置进去返回
        Page<Warehouses> warehousesPage = new Page<>(pageForm.getPageNum(), pageForm.getPageSize());
        IPage selectPage = warehousesDao.selectPage(warehousesPage, null);

        if (selectPage == null) {
            throw new sevenGroupException(40011, "查询失败");
        }
        List<WarehousesInfo> warehousesInfos = CopyProperties.copyList(selectPage.getRecords(), WarehousesInfo.class);
        selectPage.setRecords(warehousesInfos);
        return selectPage;
    }


    @Override
    public void updateWarehouses(WarehousesForm warehousesForm) throws sevenGroupException {
        if (warehousesForm == null) {
            throw new sevenGroupException(40005, "参数异常");
        }
        //修改前根据仓库地址判断仓库是否存在
        Warehouses warehouse_address = warehousesDao.selectOne(new QueryWrapper<Warehouses>().eq("warehouse_address", warehousesForm.getWarehouseAddress()));
        if (warehouse_address != null && warehouse_address.getWarehouseId() != warehousesForm.getWarehouseId()) {
            throw new sevenGroupException(40020, "该仓库已存在");
        }
        //修改前根据仓库名称判断仓库是否存在
        Warehouses warehouse_name = warehousesDao.selectOne(new QueryWrapper<Warehouses>().eq("warehouse_name", warehousesForm.getWarehouseName()));
        if (warehouse_name != null && warehouse_name.getWarehouseId() != warehousesForm.getWarehouseId()) {
            throw new sevenGroupException(40020, "该仓库已存在");

        }

        Warehouses warehouses = CopyProperties.copyOne(warehousesForm, Warehouses.class);

        warehousesDao.updateById(warehouses);
    }

    @Override
    public List<Warehouses> selectAllWarehousesByStoreId(Integer storeId) throws sevenGroupException {

        if (storeId == null || storeId <= 0) {

            throw new sevenGroupException(40001, "参数异常");

        }
        QueryWrapper<WarehouseStore> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id", storeId);
        //先查询出所有的中间表信息
        List<WarehouseStore> warehouseStores = warehouseStoreDao.selectList(wrapper);
        if (warehouseStores == null) {
            throw new sevenGroupException(40011, "查询失败");
        }
        //新建集合将仓库id存进去
        ArrayList<Integer> integers = new ArrayList<>();
        for (WarehouseStore warehouseStore : warehouseStores) {
            integers.add(warehouseStore.getWarehouseId());
        }
        if (integers == null || integers.size() <= 0) {
             throw new sevenGroupException(40011, "没有对应的仓库");
        }

        //存入后，不为空则查询
        QueryWrapper<Warehouses> wrapper1 = new QueryWrapper<>();
        wrapper1.in("warehouse_id", integers);
        List<Warehouses> warehouses = warehousesDao.selectList(wrapper1);
        if (warehouses == null) {
            throw new sevenGroupException(40011, "查询失败");
        }

        return warehouses;
    }

    @Override
    public IPage selectAllWarehouses(SelectWarehousesForm selectWarehousesForm) throws sevenGroupException {

        selectWarehousesForm.setPageNum(selectWarehousesForm.getPageNum() == null ? 1 : selectWarehousesForm.getPageNum());
        selectWarehousesForm.setPageSize(selectWarehousesForm.getPageSize() == null ? 5 : selectWarehousesForm.getPageSize());
        Page warehousesPage = new Page<>(selectWarehousesForm.getPageNum(), selectWarehousesForm.getPageSize());
        //动态sql查询所有仓库，将字符串处理一下
        selectWarehousesForm.setCondition("%" + selectWarehousesForm.getCondition() + "%");

        IPage iPage = warehousesDao.selectAllWarehouses(warehousesPage, selectWarehousesForm);

        if (iPage == null) {
            throw new sevenGroupException(40012, "查询失败");

        }

        return iPage;
    }

    @Override
    public void delWarehouses(Integer warehouseId) throws sevenGroupException {
        if (warehouseId == null || warehouseId <= 0) {
            throw new sevenGroupException(40001, "参数异常");
        }
        warehousesDao.deleteById(warehouseId);
        QueryWrapper<WarehouseStore> wrapper = new QueryWrapper<>();
        wrapper.eq("warehouse_id", warehouseId);
        warehouseStoreDao.delete(wrapper);
    }

    @Override
    public void delWarehousesByList(List<Integer> list) throws sevenGroupException {
        if (list == null || list.size() <= 0) {
            throw new sevenGroupException(40001, "参数为空");
        }

        warehousesDao.deleteBatchIds(list);
        QueryWrapper<WarehouseStore> wrapper = new QueryWrapper<>();
        wrapper.in("warehouse_id", list);
        warehouseStoreDao.delete(wrapper);
    }


}
